//
//  ACECourseListViewController.m
//  ACE
//
//  Created by zhangtao on 2018/8/9.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACECourseListViewController.h"
#import "ACEQuestionsNavTitleView.h"
#import "ACECourseListTableViewCell.h"
#import "ACESelectPickerView.h"
#import "ACEStudentDetailListController.h"
#import "ACEAddNewCourseViewController.h"

@interface ACECourseListViewController ()
@property(nonatomic,strong)UIView *coverView;

@end

@implementation ACECourseListViewController
{
    ACEQuestionsNavTitleView *titleView;
    ACESelectPickerView *pickerView;
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

-(ACESelectPickerView *)pickerView{
    if (!pickerView) {
        pickerView = [[ACESelectPickerView alloc]initWithFrame:CGRectMake(0, navHeight, ACEWidth, 200)];
        
        pickerView.dataArray = @[@"加伦比亚大学",@"哈佛大学",@"纽约大学",@"哈斯顿职业技术学院",@"加伦比亚大学",@"哈佛大学",@"纽约大学",@"哈斯顿职业技术学院",@"加伦比亚大学",@"哈佛大学",@"纽约大学",@"哈斯顿职业技术学院"];
        
        __weak typeof(titleView)weakTitleView = titleView;
        WEAKSELF;
        pickerView.SelectPickerCallBack = ^(NSString *str) {
            weakTitleView.title = str;
            [weakSelf selectSchool];
        };
    }
    return pickerView;
}

-(UIView *)coverView{
    if (!_coverView) {
        _coverView = [[UIView alloc]initWithFrame:CGRectMake(0, navHeight-2, ACEWidth, ACEHeight)];
        _coverView.backgroundColor = RGB(230, 230, 230, .5);
    }
    return _coverView;
}

-(ACEQuestionsNavTitleView *)titleView{
    if (!titleView) {
        titleView = [[ACEQuestionsNavTitleView alloc]initWithFrame:CGRectMake(30, 0, ACEWidth-60, 40)];
        WEAKSELF;
        titleView.QuestionsNavtitleCallBack = ^{
            [weakSelf selectSchool];
        };
    }
    return titleView;
}

-(void)itemClick:(UIBarButtonItem *)item{
    if (item.tag == rightItemClick) {
        NSLog(@"添加课程");
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ACEAddNewCourseViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ACEAddNewCourseViewController"];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程";
    [self.tableView registerClass:[ACECourseListTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.navigationItem.titleView = self.titleView;
    
    [self showLeftHome];
    
    if (![ACEPublicTool currentRoleIsStudent]) {
        [self initNavRightWithImage:@"Group 5 Copy 2"];
    }
}


-(void)selectSchool{
    if (_coverView) {
        [_coverView removeFromSuperview];
        _coverView = nil;
        [pickerView removeFromSuperview];
        pickerView = nil;
    }else{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self.coverView];
        [window addSubview:self.pickerView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 15;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //138:23
    return ACEWidth/(690/115);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ACECourseListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ACECourseListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ACEStudentDetailListController *student = [[ACEStudentDetailListController alloc]init];
    student.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:student animated:YES];
}

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos)
{
    // delete action
    UIContextualAction *action0 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal
                                                                          title:@"确认上课"
                                                                        handler:^(UIContextualAction * _Nonnull action,
                                                                                  __kindof UIView * _Nonnull sourceView,
                                                                                  void (^ _Nonnull completionHandler)(BOOL))
                                   {
                                       
                                       [tableView setEditing:NO animated:YES];  // 这句很重要，退出编辑模式，隐藏左滑菜单
                                       completionHandler(true);
                                   }];
    action0.backgroundColor = HEXCOLOR(0x2FCD58);
    UIContextualAction *action1 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal
                                                                          title:@"编辑"
                                                                        handler:^(UIContextualAction * _Nonnull action,
                                                                                  __kindof UIView * _Nonnull sourceView,
                                                                                  void (^ _Nonnull completionHandler)(BOOL))
                                   {
                                       
                                       [tableView setEditing:NO animated:YES];  // 这句很重要，退出编辑模式，隐藏左滑菜单
                                       completionHandler(true);
                                   }];
    action1.backgroundColor = HEXCOLOR(0xFDAC50);
    UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal
                                                                          title:@"删除"
                                                                        handler:^(UIContextualAction * _Nonnull action,
                                                                                  __kindof UIView * _Nonnull sourceView,
                                                                                  void (^ _Nonnull completionHandler)(BOOL))
                                   {
                                       
                                       [tableView setEditing:NO animated:YES];  // 这句很重要，退出编辑模式，隐藏左滑菜单
                                       completionHandler(true);
                                   }];
    action2.backgroundColor = HEXCOLOR(0xD54E51);
    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action2,action1,action0]];
    actions.performsFirstActionWithFullSwipe = NO;
    
    return actions;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"确认上课" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
    }];
    action0.backgroundColor = HEXCOLOR(0x2FCD58);
    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
    }];
    action1.backgroundColor = HEXCOLOR(0xFDAC50);
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
    }];
    action2.backgroundColor = HEXCOLOR(0xD54E51);
    return @[action2,action1,action0];
}

@end
